package com.springboot.service.wangyi;

import com.springboot.dao.CommentDao;
import com.springboot.dao.MusicDao;
import com.springboot.entity.CommentEntity;
import com.springboot.entity.MusicEntity;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import jakarta.annotation.Resource;
import java.util.Map;

/**
 *
 * 主要用于将XXX html页面解析出的数据存储到mysql数据库
 */
@Component
public class NetEaseMusicPipeline implements Pipeline {

	@Resource
	public MusicDao mMusicDao;

	@Resource
	public CommentDao mCommentDao;

	@Override
	public void process(ResultItems resultItems, Task task) {

		for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
			if (entry.getKey().equals("music")) {
				MusicEntity music = (MusicEntity) entry.getValue();
				System.out.println("mMusicDao--->null" + mMusicDao == null);
				if (mMusicDao.countBySongId(music.getSongId()) == 0) {
					mMusicDao.insert(music);
				}
			} else {
				CommentEntity comment = (CommentEntity) entry.getValue();
				System.out.println("mCommentDao--->null" + mCommentDao == null);
				if (mCommentDao.countByCommentId(comment.getCommentId()) == 0) {
					mCommentDao.insert(comment);
				}
			}

		}
	}


}
